package net.lastcoder.ifserver.web;

import net.lastcoder.ifserver.util.StringUtils;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**处理http请求
 *
 * auth: WenYF
 * date: 2017/3/1
 */
public class BaseHttpHandler extends AbstractHandler {
    private static final Logger logger = LoggerFactory.getLogger(BaseHttpHandler.class);
    private WebManager webManager;
    public BaseHttpHandler(WebManager webManager) {
        this.webManager = webManager;
    }

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request
            , HttpServletResponse response) throws IOException, ServletException {
        logger.debug("path:" + target);
//        Log.debug("request:" + baseRequest.toString());
//        Log.debug("servlet request:" + request);
//        Log.debug("servlet respone:" + response);
        AbstractHandler handler = webManager.getHandler(StringUtils.findHead(target));
        if (handler != null) {
            handler.handle(target, baseRequest, request, response);
        }
    }
}
